<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="auto">
      <el-form-item label="ID" prop="id">
        <el-input v-model="queryParams.id" placeholder="请输入ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="queryParams.username" placeholder="请输入用户名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="应用id" prop="appid">
        <el-input v-model="queryParams.appid" placeholder="请输入应用id" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="组织机构代码" prop="OperatorID">
        <el-input v-model="queryParams.OperatorID" placeholder="请输入组织机构代码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="机构全称" prop="OperatorName">
        <el-input v-model="queryParams.OperatorName" placeholder="请输入机构全称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="运营商手机号码" prop="OperatorTel1">
        <el-input v-model="queryParams.OperatorTel1" placeholder="请输入运营商手机号码" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in home_common_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          v-hasPermi="['car:operatorinfo:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate"
          v-hasPermi="['car:operatorinfo:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['car:operatorinfo:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport"
          v-hasPermi="['car:operatorinfo:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="operatorinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="组织机构代码" align="center" prop="OperatorID" />
      <el-table-column label="机构全称" align="center" prop="OperatorName" />
      <el-table-column label="运营商手机号码" align="center" prop="OperatorTel1" />
      <el-table-column label="运营商注册地址" align="center" prop="OperatorRegAddress" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <el-switch v-model="scope.row.status" active-value="1" inactive-value="0"
            @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="应用id" align="center" prop="appid" />
      <el-table-column label="域名前缀" align="center" prop="webUrl" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['car:operatorinfo:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['car:operatorinfo:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改运营商管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="operatorinfoRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="登录密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入登录密码" />
        </el-form-item>
        <el-form-item label="组织机构代码" prop="OperatorID">
          <el-input v-model="form.OperatorID" placeholder="请输入组织机构代码" />
        </el-form-item>
        <el-form-item label="机构全称" prop="OperatorName">
          <el-input v-model="form.OperatorName" placeholder="请输入机构全称" />
        </el-form-item>
        <el-form-item label="运营商手机号码" prop="OperatorTel1">
          <el-input v-model="form.OperatorTel1" placeholder="请输入运营商手机号码" />
        </el-form-item>
        <el-form-item label="运营商注册地址" prop="OperatorRegAddress">
          <el-input v-model="form.OperatorRegAddress" placeholder="请输入运营商注册地址" />
        </el-form-item>
        <el-form-item label="备注信息" prop="OperatorNote">
          <el-input v-model="form.OperatorNote" placeholder="请输入备注信息" />
        </el-form-item>
        <el-form-item label="域名前缀" prop="webUrl">
          <el-input v-model="form.webUrl" placeholder="请输入域名前缀" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Operatorinfo">
import { listOperatorinfo, getOperatorinfo, delOperatorinfo, addOperatorinfo, updateOperatorinfo,changeStatus } from "@/api/car/operatorinfo";

const { proxy } = getCurrentInstance();
const { home_common_status } = proxy.useDict('home_common_status');

const operatorinfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    username: null,
    OperatorID: null,
    OperatorName: null,
    OperatorTel1: null,
    status: null,
    appid: null,
  },
  rules: {
    username: [
      { required: true, message: "用户名不能为空", trigger: "blur" }
    ],
    password: [
      { required: true, message: "登录密码不能为空", trigger: "blur" }
    ],
    OperatorID: [
      { required: true, message: "组织机构代码不能为空", trigger: "blur" }
    ],
    OperatorName: [
      { required: true, message: "机构全称不能为空", trigger: "blur" }
    ],
    OperatorTel1: [
      { required: true, message: "运营商手机号码不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询运营商管理列表 */
function getList() {
  loading.value = true;
  listOperatorinfo(queryParams.value).then(response => {
    operatorinfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    username: null,
    password: null,
    OperatorID: null,
    OperatorName: null,
    OperatorTel1: null,
    OperatorRegAddress: null,
    OperatorNote: null,
    createTime: null,
    updateTime: null,
    status: null,
    appid: null,
    isCodeRules: null,
    webUrl: null
  };
  proxy.resetForm("operatorinfoRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加运营商管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getOperatorinfo(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改运营商管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["operatorinfoRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateOperatorinfo(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addOperatorinfo(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除运营商管理编号为"' + _ids + '"的数据项？').then(function () {
    return delOperatorinfo(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('car/operatorinfo/export', {
    ...queryParams.value
  }, `operatorinfo_${new Date().getTime()}.xlsx`)
}
/** 状态修改  */
function handleStatusChange(row) {
  let text = row.status === "1" ? "启用" : "停用";
  proxy.$modal.confirm('确认要"' + text + '""' + row.username + '"用户吗?').then(function () {
    return changeStatus(row.userId, row.status);
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功");
  }).catch(function () {
    row.status = row.status === "1" ? "1" : "0";
  });
};
getList();
</script>
